//
//  CAAnimationGroupViewController.m
//  iOSHeartAnimation
//
//  Created by ddy on 2018/7/27.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "CAAnimationGroupViewController.h"
#define KRadian(angle) ((angle) / 180.0 * M_PI)
@interface CAAnimationGroupViewController ()<CAAnimationDelegate>
/** 红色View */
@property (nonatomic, strong) UIView  *redView;
@end
static NSString * const kGroupPosition = @"kGroupPositionKey";
static NSString * const KGroupAnimation = @"KGroupAnimationKey";
@implementation CAAnimationGroupViewController
/** 红色View */
- (UIView *)redView{
    if(!_redView){
        _redView = [UIView new];
        _redView.layer.cornerRadius = 30;
        _redView.layer.masksToBounds = YES;
        _redView.backgroundColor = [UIColor redColor];
    }
    return  _redView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"组合动画";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.redView];
    self.redView.frame = CGRectMake(150, 100, 100, 100);
    //动画
    [self groupAniamtion:CGPointMake(130, 200)];
    
    
    
}
#pragma mark --- 动画
- (void)groupAniamtion:(CGPoint)point {
    //1.晃动动画CAKeyFrameAnimation
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    keyAnimation.values = @[@KRadian(-4),@KRadian(4),@KRadian(-4)];
    //每一个动画可以单独设置时间和重复次数,在动画组的时间基础上,控制单动画的效果
    keyAnimation.duration = 0.3;
    keyAnimation.repeatCount = MAXFLOAT;
    keyAnimation.delegate = self;
    
    //2.位移动画 CABaseAnimation
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    //到达位置
    baseAnimation.byValue = [NSValue valueWithCGPoint:point];
    baseAnimation.duration = 2.0;
    baseAnimation.repeatCount = 1.0;
    baseAnimation.removedOnCompletion = NO;
    baseAnimation.fillMode = kCAFillModeForwards;
    baseAnimation.delegate = self;
    //动画节奏
    baseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //3.CASpringAnimtion
    CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"position"];
    springAnimation.damping = 20;
    springAnimation.stiffness = 500.0;
    springAnimation.mass = 10;
    springAnimation.initialVelocity = 10.0;
    springAnimation.delegate = self;
    springAnimation.duration = springAnimation.settlingDuration;
    springAnimation.toValue = [NSValue valueWithCGPoint:point];
    //4.动画组
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.animations = @[baseAnimation,keyAnimation,springAnimation];
    animationGroup.delegate = self;
    ///动画之行后回到初始位置
    animationGroup.autoreverses = YES;
    //动画的表现时间和重复次数由动画组设置的决定
    animationGroup.duration = 5.0;
    animationGroup.repeatCount = 1;
    animationGroup.timeOffset = 0 ;
    animationGroup.removedOnCompletion = NO;
    animationGroup.fillMode = kCAFillModeForwards;
    [animationGroup setValue:[NSValue valueWithCGPoint:point] forKey:kGroupPosition];
    [self.redView.layer addAnimation:animationGroup forKey:KGroupAnimation];
    
    
    
    
    
}

///触动
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [touches.anyObject locationInView:self.view];
    [self groupAniamtion:point];
}
#pragma mark --- <KCAAnimationDelegate>
- (void)animationDidStart:(CAAnimation *)anim{
    CAAnimation *animation = [self.redView.layer animationForKey:KGroupAnimation];
    if (animation == anim) {
        NSLog(@"动画开始");
    }
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    CAAnimation *animation = [self.redView.layer animationForKey:KGroupAnimation];
    //    animationGroup.removedOnCompletion = NO;
//    animationGroup.fillMode = kCAFillModeForwards;才会animation不为nil
    if ( animation == anim) {
        
        NSLog(@"动画结束");
        //事物
        [CATransaction begin];
        [CATransaction setDisableActions:YES];
        self.redView.layer.position = [[anim valueForKey:kGroupPosition]CGPointValue];
        [CATransaction commit];
        
    }
   
}














@end
